# Makefile for LibRHash
CC      = gcc
AR      = ar
#NOTE: NDEBUG turns off asserts
OPTFLAGS   = -O2 -DNDEBUG -fomit-frame-pointer -ffunction-sections -fdata-sections
OPTLDFLAGS =
CFLAGS     = $(OPTFLAGS)
LDFLAGS    = $(OPTLDFLAGS)
ADDCFLAGS  =
LIBCFLAGS  =
ADDLDFLAGS =
DEFS    = -DIN_RHASH
PRGCFLAGS  = -pipe $(DEFS) $(ADDCFLAGS) $(CFLAGS) \
  -Wall -W -Wstrict-prototypes -Wnested-externs -Winline -Wpointer-arith \
  -Wbad-function-cast -Wmissing-prototypes -Wmissing-declarations
ALLCFLAGS = $(LIBCFLAGS) $(PRGCFLAGS)
LDRHASH    = -L. -lrhash
LIBLDFLAGS = $(LDFLAGS) $(ADDLDFLAGS)
HEADERS = algorithms.h byte_order.h plug_openssl.h rhash.h rhash_timing.h util.h aich.h crc32.h ed2k.h edonr.h hex.h md4.h md5.h sha1.h sha256.h sha512.h sha3.h ripemd-160.h gost.h has160.h snefru.h tiger.h              tth.h torrent.h whirlpool.h
SOURCES = algorithms.c byte_order.c plug_openssl.c rhash.c rhash_timing.c util.c aich.c crc32.c ed2k.c edonr.c hex.c md4.c md5.c sha1.c sha256.c sha512.c sha3.c ripemd-160.c gost.c has160.c snefru.c tiger.c tiger_sbox.c tth.c torrent.c whirlpool.c whirlpool_sbox.c
OBJECTS = algorithms.o byte_order.o plug_openssl.o rhash.o rhash_timing.o util.o aich.o crc32.o ed2k.o edonr.o hex.o md4.o md5.o sha1.o sha256.o sha512.o sha3.o ripemd-160.o gost.o has160.o snefru.o tiger.o tiger_sbox.o tth.o torrent.o whirlpool.o whirlpool_sbox.o
LIB_HEADERS = rhash.h rhash_timing.h
# installation directories and names
DESTDIR =
PREFIX  = /usr/local
INCDIR  = $(PREFIX)/include
LIBDIR  = $(PREFIX)/lib
LIBRARY = librhash.a
SONAME  = librhash.so.0
SOLINK  = librhash.so
TEST_TARGET = test_hashes
TEST_SHARED = test_shared
# Set variables according to GNU coding standard
INSTALL = install
INSTALL_DATA    = $(INSTALL) -m 644
INSTALL_SHARED  = $(INSTALL) -m 644

all: $(LIBRARY)
lib-static: $(LIBRARY)
lib-shared: $(SONAME)
libs-all: lib-static lib-shared
dist-clean: clean

install-lib-static: $(LIBRARY) install-headers
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL_DATA) $(LIBRARY) $(DESTDIR)$(LIBDIR)/
	ln -s $(SONAME) $(DESTDIR)$(LIBDIR)/$(SOLINK)

install-lib-shared: $(SONAME)
	$(INSTALL) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL_SHARED) $(SONAME) $(DESTDIR)$(LIBDIR)/

uninstall-lib-static: uninstall-headers
	rm -f $(DESTDIR)$(LIBDIR)/$(LIBRARY)
	rm -f $(DESTDIR)$(LIBDIR)/$(SOLINK)

uninstall-lib-shared:
	rm -f $(DESTDIR)$(LIBDIR)/$(SONAME)

install-headers:
	$(INSTALL) -d $(DESTDIR)$(INCDIR)
	$(INSTALL_DATA) $(LIB_HEADERS) $(DESTDIR)$(INCDIR)/

uninstall-headers:
	for f in $(LIB_HEADERS); do rm -f "$(DESTDIR)$(INCDIR)/$$f"; done

# not using GNU make extensions for compatibility with Unix/*BSD make
#%.o: %.c
#	$(CC) -c $(ALLCFLAGS) $< -o $@

# NOTE: dependences were generated by 'gcc -MM -DIN_RHASH -DUSE_OPENSSL *.c'
# we are using plain old makefile style to support BSD make
aich.o: aich.c byte_order.h ustd.h config.h algorithms.h rhash.h aich.h \
 sha1.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

algorithms.o: algorithms.c byte_order.h ustd.h config.h rhash.h \
 algorithms.h aich.h sha1.h crc32.h ed2k.h md4.h edonr.h gost.h has160.h \
 md5.h ripemd-160.h snefru.h sha256.h sha512.h sha3.h tiger.h torrent.h \
 tth.h whirlpool.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

byte_order.o: byte_order.c byte_order.h ustd.h config.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

crc32.o: crc32.c byte_order.h ustd.h config.h crc32.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

ed2k.o: ed2k.c ed2k.h md4.h ustd.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

edonr.o: edonr.c byte_order.h ustd.h config.h edonr.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

gost.o: gost.c byte_order.h ustd.h config.h gost.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

has160.o: has160.c byte_order.h ustd.h config.h has160.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

hex.o: hex.c hex.h ustd.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

md4.o: md4.c byte_order.h ustd.h config.h md4.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

md5.o: md5.c byte_order.h ustd.h config.h md5.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

plug_openssl.o: plug_openssl.c algorithms.h rhash.h byte_order.h ustd.h \
 config.h plug_openssl.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

rhash.o: rhash.c byte_order.h ustd.h config.h algorithms.h rhash.h \
 torrent.h sha1.h plug_openssl.h util.h hex.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

rhash_timing.o: rhash_timing.c byte_order.h ustd.h config.h rhash.h \
 rhash_timing.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

ripemd-160.o: ripemd-160.c byte_order.h ustd.h config.h ripemd-160.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

sha1.o: sha1.c byte_order.h ustd.h config.h sha1.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

sha256.o: sha256.c byte_order.h ustd.h config.h sha256.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

sha512.o: sha512.c byte_order.h ustd.h config.h sha512.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

sha3.o: sha3.c byte_order.h ustd.h config.h sha3.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

snefru.o: snefru.c byte_order.h ustd.h config.h snefru.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

test_hashes.o: test_hashes.c byte_order.h ustd.h config.h rhash_timing.h \
 rhash.h test_hashes.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

tiger.o: tiger.c byte_order.h ustd.h config.h tiger.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

tiger_sbox.o: tiger_sbox.c byte_order.h ustd.h config.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

torrent.o: torrent.c byte_order.h ustd.h config.h algorithms.h rhash.h \
 hex.h torrent.h sha1.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

tth.o: tth.c byte_order.h ustd.h config.h tth.h tiger.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

util.o: util.c util.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

whirlpool.o: whirlpool.c byte_order.h ustd.h config.h whirlpool.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

whirlpool_sbox.o: whirlpool_sbox.c byte_order.h ustd.h config.h
	$(CC) -c $(ALLCFLAGS) $< -o $@

# MINGW dll target
DLLNAME = librhash.dll

dll: $(DLLNAME)
$(DLLNAME): $(SOURCES)
	sed -n '1s/.*/{ global:/p; s/^RHASH_API.* \([a-z0-9_]\+\)(.*/  \1;/p; $$s/.*/local: *; };/p' $(LIB_HEADERS) > exports.sym
	$(CC) -DRHASH_EXPORTS $(ALLCFLAGS) -shared $(SOURCES) -Wl,--version-script,exports.sym,--output-def,librhash.def,-soname,$@ $(LIBLDFLAGS) -o $@

test-dll: $(DLLNAME) test_hashes.o
	$(CC) $(PRGCFLAGS) test_hashes.o $(DLLNAME) -o $(TEST_SHARED) && ./$(TEST_SHARED)

# shared and static libraries
$(SONAME): $(SOURCES)
	sed -n '1s/.*/{ global:/p; s/^RHASH_API.* \([a-z0-9_]\+\)(.*/  \1;/p; $$s/.*/local: *; };/p' $(LIB_HEADERS) > exports.sym
	$(CC) -fpic $(ALLCFLAGS) -shared $(SOURCES) -Wl,--version-script,exports.sym,-soname,$(SONAME) $(LIBLDFLAGS) -o $@
	ln -s $(SONAME) $(SOLINK)
# use 'nm -Cg --defined-only $@' to view exported symbols

$(LIBRARY): $(OBJECTS)
	$(AR) cqs $@ $(OBJECTS)
#	$(AR) cr $(LIBRARY) $(OBJECTS) && runlib $(LIBRARY)

$(TEST_TARGET): $(LIBRARY) test_hashes.o
	$(CC) $(ALLCFLAGS) test_hashes.o $(LIBRARY) $(LIBLDFLAGS) -o $@

test-shared: $(SONAME) test_hashes.o
	$(CC) $(PRGCFLAGS) test_hashes.o $(SONAME) $(LIBLDFLAGS) -o $(TEST_SHARED)
	LD_LIBRARY_PATH=.:$(LD_LIBRARY_PATH) ./$(TEST_SHARED)

test-static: $(TEST_TARGET)
	./$(TEST_TARGET)

test: test-static
	if [ -f $(SONAME) ]; then make test-shared; fi
#	if [ -f $(DLLNAME) ]; then make test-dll; fi

clean:
	rm -f *.o $(LIBRARY) $(TEST_TARGET) $(TEST_SHARED) $(SONAME) $(SOLINK) exports.sym $(DLLNAME) librhash.def
